<template>
  <el-form ref="formRef" :model="form" label-width="180px" size="default">
    <el-form-item>
      <h2>
        <el-link type="warning">申请超过12小时未核收试管标记异常</el-link>
      </h2>
    </el-form-item>
    <el-form-item label="医院名称：">
      <el-select v-model="form.value" clearable filterable placeholder="请选择">
        <el-option v-for="item in form.options" :key="item.id" :label="item.name" :value="item.id"> </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="时间范围：">
      <el-date-picker
        v-model="form.timeArr"
        type="datetimerange"
        :editable="false"
        format="YYYY-MM-DD HH:mm:ss"
        value-format="YYYY-MM-DD HH:mm:ss"
        range-separator="至"
        start-placeholder="开始时间"
        end-placeholder="结束时间"
      >
      </el-date-picker>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit">生成脚本</el-button>
      <el-button type="default" @click="exportFile">导出文件</el-button>
    </el-form-item>
    <el-form-item label="脚本：">
      <el-input autosize type="textarea" v-model="form.scriptString" readonly></el-input>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { ref, reactive } from "vue";
import { format } from "./js/timeFromat";
import saveAs from "@/views/script-generator/js/FileSaver";
import instArr from "./js/instArr";
// do not use same name with ref
const form = reactive({
  // 医院id
  value: "",
  // 机构列表
  options: instArr,
  // 时间范围
  timeArr: [],
  // 脚本
  scriptString: ""
});

const exportFile = () => {
  let str = new Blob([form.scriptString], {
    type: "text/plain;charset=utf-8"
  });
  let instName = instArr.filter((item) => item.id === form.value)[0].name;
  saveAs(str, `${instName}-${form.timeArr[0]}至${form.timeArr[1]}.txt`);
};

const onSubmit = () => {
  form.scriptString = `环境：太极政务云
数据库地址：192.159.107.33:2000
数据库名：nat_admin

先备份：（请将备份的数据写在jira的备注里，如果数据多，可以生成txt文件上传到jira里）
var c = db.nat_tube.find(
{$and:[{"check_inst_id":"${form.value}"},{"create_time":{"$gte":"${form.timeArr[0]}","$lte":"${form.timeArr[1]}"}},{"status":{"$in":[1]}}]}
)
d = []
while(c.hasNext()) {
d.push(c.next())
}
printjson(d)


再执行  （打印出来的是在redis运行的语句）
db.nat_tube.find(
{$and:[{"check_inst_id":"${form.value}"},{"create_time":{"$gte":"${form.timeArr[0]}","$lte":"${form.timeArr[1]}"}},{"status":{"$in":[1]}}]}
).forEach(function (tubeObj) {
tubeObj.status = NumberInt(5);
tubeObj.abnormal_sign  = NumberInt(1);
db.getCollection('nat_tube').save(tubeObj);
var appoCheckList = db.getCollection('nat_appo_check').find({"tube_id": tubeObj.id}).
forEach( function ( appoCheckObj) {
db.getCollection('nat_appo_check').update({"tube_id":tubeObj.id,id:appoCheckObj.id},{$set:{
status : NumberInt(5),
abnormal_sign:NumberInt(1),
}},false,true);
print("del appointment."+appoCheckObj.appo_person);
print("del appointment."+appoCheckObj.appo_unique_code);
var relationObj = db.getCollection('nat_appo_check_relation').findOne({"appo_check_id": appoCheckObj.id});
if(relationObj != null){
relationObj.appo_status = NumberInt(5);
relationObj.abnormal_sign = NumberInt(1);
db.getCollection('nat_appo_check_relation').save(relationObj);
}
});
});`;
};
</script>

<style>
.input-width {
  width: 400px;
}
</style>
